package com.thenmorethings.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.thenmorethings.entity.Essay;
import com.thenmorethings.entity.PageImg;
import com.thenmorethings.service.EssayService;
import com.thenmorethings.service.ImgService;

public class PageAction extends ActionSupport {

	@Resource
	private ImgService imgService;
	
	@Resource
	private EssayService essayService;
	
	
	
	private List<Essay> EssayList;
	
	private Essay essay;
	
	public String pageIndex() throws Exception
	{
		List<PageImg> Imglist = imgService.findAll();
		
		
		List<PageImg> list = new ArrayList<PageImg>();
		list.add(Imglist.get(Imglist.size()-1));
		list.add(Imglist.get(Imglist.size()-2));
		list.add(Imglist.get(Imglist.size()-3));
		list.add(Imglist.get(Imglist.size()-4));
		/*
		 * 每次都取最后三个值 加入一个新的集合 再转成json对象
		 * 返回给界面
		 */
		
		JSONArray jSONArray = JSONArray.fromObject(list);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw = response.getWriter();
		pw.write(jSONArray.toString());
		pw.flush();
		pw.close();
		
		return null;
	}
	
	
	/*
	 * 页面的中转函数
	 */
	public String pageIndex1()
	{		
		return "index";
	}
	
	/*
	 * 主页点击标题跳转到另外一个界面 显示内容
	 * 
	 * 
	 */
	public String Findessay()
	{
		if(essay!=null)
		{
			essay = essayService.EssayFind(essay.getId());
			return "essaydisplay";
		}
		return "error"; //跳转到文章的显示界面
	}
	
	public void SystemRefresh()
	{
		EssayList = essayService.findAll();
		ServletContext application = ServletActionContext.getServletContext();
		application.setAttribute("EssayList", EssayList);
	}
	
	
	
	public List<Essay> getEssayList() {
		return EssayList;
	}
	public void setEssayList(List<Essay> essayList) {
		EssayList = essayList;
	}
	public Essay getEssay() {
		return essay;
	}
	public void setEssay(Essay essay) {
		this.essay = essay;
	}
	
	

	


	
}
